<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 查询区域 -->
      <div class="search-box">
        <el-form-item label="单位名称" prop="unitName">
          <el-input
            v-model="queryParams.unitName"
            placeholder="请输入单位名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.erp_enable_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"/>
          </el-select>
        </el-form-item>
      </div>
      <!--按钮组-->
      <div class="search-btn">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-form-item>
      </div>
      <!--数据展示区-->
      <el-table v-loading="loading" :data="productUnitList">
        <el-table-column label="序号" align="center" prop="unitId"/>
        <el-table-column label="单位名称" align="center" prop="unitName"/>
        <el-table-column label="状态" align="center" prop="isActive">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.erp_enable_disable" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDisable(scope.row)"
              v-hasPermi="['system:post:remove']"
            >{{ scope.row.status === 'Y' ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <!-- 添加或修改仓库信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="单位名称" prop="unitName">
            <el-input v-model="form.unitName" placeholder="请输入单位名称"/>
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择" clearable style="width: 240px">
              <el-option v-for="dict in dict.type.erp_enable_disable"
                         :key="dict.value"
                         :label="dict.label"
                         :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import {addProductUnit, getProductUnit, getProductUnitList, updateProductUnit} from "@/api/erp/productunit"
import {updateBrand} from "@/api/erp/productbrand";

export default {
  name: "",
  dicts: ['erp_enable_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: '',
        status: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      productUnitList: [],
      allList: [],
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        unitName: [
          {required: true, message: "商品单位名称不能为空", trigger: "blur"}
        ]
      }
    }
  },

  created() {
    this.getList()
  },
  methods: {
    /** 查询商品单位列表 */
    getList() {
      // 遮罩层开启
      this.loading = true
      // 调用接口
      getProductUnitList(this.queryParams).then(response => {
        this.productUnitList = response.rows
        this.total = response.total
        // 遮罩层关闭
        this.loading = false
      })
    },
    // 查询方法
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置方法
    resetQuery() {
      // 使用RuoYi提供的方法清空查询数据
      this.resetForm("queryForm")
      // 调用查询方法再次查询
      this.handleQuery()
    },
    // 新增商品单位弹窗方法
    handleAdd() {
      this.open = true
      this.title = "添加商品单位"
    },
    // 修改商品单位弹窗方法
    handleUpdate(row) {
      this.open = true
      this.title = "修改商品单位"
      this.form = row
    },
    // 禁用商品单位方法
    handleDisable(row) {
      if (row.status === 'Y') {
        this.$modal.confirm('请确认是否禁用该商品单位').then(function () {
          row.status = 'N'
          // 调用更新方法
          return updateProductUnit(row)
        }).catch(() => {
        })
        console.log("禁用方法")
      } else {
        this.$modal.confirm('请确认是否启用该商品单位').then(function () {
          row.status = 'Y'
          // 调用更新方法
          return updateProductUnit(row)
        }).catch(() => {
        })
        console.log("启用方法")
      }
    },
    // 弹出层保存按钮方法
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === '修改商品单位') {
            console.log(this.form)
            updateProductUnit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.status = 'Y'
            addProductUnit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 弹出层取消按钮方法
    cancel() {
      // 关闭弹出层
      this.open = false
      // 清空表单数据
      this.reset()
    },
    // 表单重置方法
    reset() {
      this.form = {
        unitName: undefined,
      }
      this.resetForm("form")
    },
  }
}
</script>

<style>

</style>
